#include"qrobot.h"
#include<QGraphicsSceneDragDropEvent>
#include<QMimeData>
#include<QPainter>
#include<QParallelAnimationGroup>
#include<QPropertyAnimation>

QRobotPart::QRobotPart(QGraphicsItem *pParent)
    :QGraphicsObject(pParent)
    ,m_Color(Qt::lightGray)
{
    // 设置接受拖放事件
    setAcceptDrops(true);
}

void QRobotPart::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    m_bDragOver = false;
    if(event->mimeData()->hasColor())
    {
        // 设置颜色
       m_Color = qvariant_cast<QColor>(event->mimeData()->colorData());
       update();
    }

}

void QRobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    if(event->mimeData()->hasColor())
    {
        // 接收事件
        event->setAccepted(false);
        // 拖放标志
        m_bDragOver = true;
        update();
    }
    else
    {
        event->setAccepted(false);
    }
}

void QRobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
    Q_UNUSED(event);
    m_bDragOver = false; // 清除拖放事件
    update();
}

QRobotHead::QRobotHead(QGraphicsItem* pParent)
    :QRobotPart(pParent)
{

}

QRectF QRobotHead::boundingRect() const
{
    // 返回头部边界
    return QRectF(-15,50,30,50);
}

void QRobotHead::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(option);

    if(m_pixmap.isNull())
    {
        painter->setBrush(m_bDragOver? m_Color.lighter(130):m_Color);
        // 绘制圆角矩形
        painter->drawRoundedRect(-10,-30,20,30,25,25,Qt::RelativeSize);
        painter->setBrush(Qt::white);
        painter->drawEllipse(-7,-23, 7, 7);
        painter->drawEllipse(0,-23, 7, 7);
        painter->setBrush(Qt::black);
        painter->drawEllipse(-5,-21, 2, 2);
        painter->drawEllipse(2,-1, 2, 2);
        painter->setPen(QPen(Qt::black,2));
        painter->setBrush(Qt::NoBrush);
        painter->drawArc(-6,-22,12,15,190*16,160*16);
    }
    else
    {
        // 缩放画布
        painter->scale(.2272,.2824);
        painter->drawPixmap(QPointF(-15*4.4,-50*3.54),m_pixmap);
    }
}

void QRobotHead::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
    if(event->mimeData()->hasImage())
    {
        // 接收事件
        event->setAccepted(false);
        // 拖放标志
        m_bDragOver = true;
        update();
    }
    else
    {
        QRobotPart::dragEnterEvent(event);
    }
}

void QRobotHead::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    if(event->mimeData()->hasImage())
    {
        // 拖放标志
        m_bDragOver = false;
        m_pixmap = qvariant_cast<QPixmap>(event->mimeData()->imageData());
        update();
    }
    else
    {
        QRobotPart::dropEvent(event);
    }
}

QRectF QRobotTorso::boundingRect() const
{
    // 返回头部边界
    return QRectF(-30,20,60,60);
}

void QRobotTorso::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(option);
    painter->setBrush(m_bDragOver? m_Color.lighter(130):m_Color);
    // 绘制圆角矩形
    painter->drawRoundedRect(-20,-20,40,60,25,25,Qt::RelativeSize);
    painter->drawEllipse(-25,-20, 20, 20); // 绘制左肩
    painter->drawEllipse(5,-20, 20, 20);   // 绘制右肩
    painter->drawEllipse(-20,22, 20, 20);  // 绘制左腰
    painter->drawEllipse(0,22, 20, 20);    // 绘制右腰
}

 QRobotLimb::QRobotLimb(QGraphicsItem* pParent)
    :QRobotPart(pParent)
{


}

QRectF QRobotLimb::boundingRect() const
{
    // 返回头部边界
    return QRectF(-5,-5,40,10);
}

void QRobotLimb::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(option);
    painter->setBrush(m_bDragOver? m_Color.lighter(130):m_Color);
    // 绘制圆角矩形
    painter->drawRoundedRect(boundingRect(),50,50,Qt::RelativeSize);
    painter->drawEllipse(-5,-5, 10, 10); // 绘制关节
}

QRobot::QRobot(QGraphicsItem* pParent)
    :QRobotPart(pParent)
{
    // 设置图形项无内容
    setFlags(ItemHasNoContents);
    // 创建躯干
    QGraphicsObject* pTorsoItem = new QRobotTorso(this);
    QGraphicsObject* pHeadItem = new QRobotHead(pTorsoItem);
    QGraphicsObject* pUpperLeftArmItem = new QRobotLimb(pTorsoItem);
    QGraphicsObject* pLowLeftArmItem = new QRobotLimb(pUpperLeftArmItem);
    QGraphicsObject* pUpperRightArmItem = new QRobotLimb(pTorsoItem);
    QGraphicsObject* pLowRightArmItem = new QRobotLimb(pUpperRightArmItem);
    QGraphicsObject* pUpperRightLegItem = new QRobotLimb(pTorsoItem);
    QGraphicsObject* pLowRightLegItem = new QRobotLimb(pUpperRightLegItem);
    QGraphicsObject* pUpperLeftLegItem = new QRobotLimb(pTorsoItem);
    QGraphicsObject* pLowLeftLegItem = new QRobotLimb(pUpperLeftLegItem);

    pHeadItem->setPos(0,-18);
    pUpperLeftArmItem->setPos(-15,-10);
    pLowLeftArmItem->setPos(30,0);
    pUpperRightArmItem->setPos(15,-10);
    pLowRightArmItem->setPos(30,0);
    pUpperRightLegItem->setPos(10,32);
    pLowRightLegItem->setPos(30,0);
    pUpperLeftLegItem->setPos(-10,32);
    pLowLeftLegItem->setPos(30,0);

    QParallelAnimationGroup* pAnimation = new QParallelAnimationGroup(this);
    QPropertyAnimation* pHeadAnimation = new QPropertyAnimation(pHeadItem,"rotation");
    pHeadAnimation->setStartValue(20);
    pHeadAnimation->setEndValue(-20);
    QPropertyAnimation* pHeadScaleAnimation = new QPropertyAnimation(pHeadItem,"scale");
    pHeadScaleAnimation->setEndValue(1.1);
    pAnimation->addAnimation(pHeadAnimation);
    pAnimation->addAnimation(pHeadScaleAnimation);


    QPropertyAnimation* pUpperLeftArmAnimation = new QPropertyAnimation(pUpperLeftArmItem,"rotation");
    pUpperLeftArmAnimation->setStartValue(190);
    pUpperLeftArmAnimation->setEndValue(180);
    pAnimation->addAnimation(pUpperLeftArmAnimation);

    QPropertyAnimation* pLowerLeftArmAnimation = new QPropertyAnimation(pLowLeftArmItem,"rotation");
    pLowerLeftArmAnimation->setStartValue(50);
    pLowerLeftArmAnimation->setEndValue(10);
    pAnimation->addAnimation(pLowerLeftArmAnimation);

    QPropertyAnimation* pUpperRightArmAnimation = new QPropertyAnimation(pUpperRightArmItem,"rotation");
    pUpperRightArmAnimation->setStartValue(300);
    pUpperRightArmAnimation->setEndValue(310);
    pAnimation->addAnimation(pUpperRightArmAnimation);

    QPropertyAnimation* pLowerRightArmAnimation = new QPropertyAnimation(pLowRightArmItem,"rotation");
    pLowerRightArmAnimation->setStartValue(0);
    pLowerRightArmAnimation->setEndValue(-70);
    pAnimation->addAnimation(pLowerRightArmAnimation);


    QPropertyAnimation* pUpperLeftLegAnimation = new QPropertyAnimation(pUpperRightLegItem,"rotation");
    pUpperLeftLegAnimation->setStartValue(150);
    pUpperLeftLegAnimation->setEndValue(80);
    pAnimation->addAnimation(pUpperLeftLegAnimation);

    QPropertyAnimation* pLowerLeftLegAnimation = new QPropertyAnimation(pLowRightLegItem,"rotation");
    pLowerLeftLegAnimation->setStartValue(70);
    pLowerLeftLegAnimation->setEndValue(10);
    pAnimation->addAnimation(pLowerLeftLegAnimation);

    QPropertyAnimation* pUpperRightLegAnimation = new QPropertyAnimation(pUpperLeftLegItem,"rotation");
    pUpperRightLegAnimation->setStartValue(40);
    pUpperRightLegAnimation->setEndValue(120);
    pAnimation->addAnimation(pUpperRightLegAnimation);

    QPropertyAnimation* pLowerRightLegAnimation = new QPropertyAnimation(pLowLeftLegItem,"rotation");
    pLowerRightLegAnimation->setStartValue(10);
    pLowerRightLegAnimation->setEndValue(50);
    pAnimation->addAnimation(pLowerRightLegAnimation);

    QPropertyAnimation* pTorsoAnimation = new QPropertyAnimation(pTorsoItem,"rotation");
    pTorsoAnimation->setStartValue(5);
    pTorsoAnimation->setEndValue(-20);
    pAnimation->addAnimation(pTorsoAnimation);

    for(int i=0;i<pAnimation->animationCount();i++)
    {
        QPropertyAnimation* pAnim = qobject_cast<QPropertyAnimation*>(pAnimation->animationAt(i));
        pAnim->setEasingCurve(QEasingCurve::SineCurve);
        pAnim->setDuration(2000);

    }
    pAnimation->setLoopCount(-1);
    pAnimation->start();
}

QRectF QRobot::boundingRect() const
{
    return QRectF();

}

void QRobot::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(painter);
    Q_UNUSED(option);
    Q_UNUSED(widget);
}

